<?php

namespace app\admin\model\store;

use think\Model;


class Store extends Model
{

    

    

    // 表名
    protected $name = 'store';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'add_time';
    protected $updateTime = 'edit_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'id_card_valid_start_time_text',
        'id_card_valid_end_time_text',
        'id_card_valid_time_text',
        'subject_type_text',
        'business_time_text',
        'add_time_text',
        'edit_time_text',
        'is_ban_text',
        'is_del_text',
        'authen_time_text',
        'xvstatus_text',
        'end_time_text',
        'promotion_type_text',
        'type_text',
        'pid_text'
    ];
    

    
    public function getSubjectTypeList()
    {
        return ['1' => __('Subject_type 1'), '2' => __('Subject_type 2')];
    }

    public function getIsBanList()
    {
        return ['0' => __('Is_ban 0'), '1' => __('Is_ban 1')];
    }

    public function getIsDelList()
    {
        return ['0' => __('Is_del 0'), '1' => __('Is_del 1')];
    }

    public function getXvstatusList()
    {
        return ['0' => __('Xvstatus 0'), '1' => __('Xvstatus 1'), '2' => __('Xvstatus 2'), '3' => __('Xvstatus 3'), '4' => __('Xvstatus 4')];
    }

    public function getPromotionTypeList()
    {
        return ['0' => __('Promotion_type 0'), '1' => __('Promotion_type 1'), '2' => __('Promotion_type 2'), '3' => __('Promotion_type 3')];
    }


    public function getIdCardValidStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['id_card_valid_start_time']) ? $data['id_card_valid_start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getIdCardValidEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['id_card_valid_end_time']) ? $data['id_card_valid_end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function gettypeTextAttr($value, $data)
    {
        $arr = [1=>'中心',2=>'空间'];
        return $arr[$data['type']];
    }


    public function getIdCardValidTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['id_card_valid_time']) ? $data['id_card_valid_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSubjectTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['subject_type']) ? $data['subject_type'] : '');
        $list = $this->getSubjectTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getBusinessTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['business_time']) ? $data['business_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getAddTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['add_time']) ? $data['add_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEditTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['edit_time']) ? $data['edit_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsBanTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_ban']) ? $data['is_ban'] : '');
        $list = $this->getIsBanList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsDelTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_del']) ? $data['is_del'] : '');
        $list = $this->getIsDelList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAuthenTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['authen_time']) ? $data['authen_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getPidTextAttr($value, $data)
    {
        $storeName = db('store')->where('id',$data['pid'])->value('store_name');
        return $storeName ? $storeName : '';
    }



    public function getXvstatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['xvstatus']) ? $data['xvstatus'] : '');
        $list = $this->getXvstatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPromotionTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['promotion_type']) ? $data['promotion_type'] : '');
        $list = $this->getPromotionTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setIdCardValidStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setIdCardValidEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setIdCardValidTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setBusinessTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setAddTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEditTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setAuthenTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }




    public function admin()
    {
        return $this->belongsTo('app\admin\model\Admin', 'id', 'store_id', [], 'LEFT')->setEagerlyType(0);
    }
}
